package com.zmn.mcc.services.impl.dept;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dictionary.GlobalDict;
import com.zmn.common.dto.StatusDTO;
import com.zmn.common.dto.VtDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.mcc.model.entity.dept.DeptIdAndRoleId;
import com.zmn.mcc.model.entity.dept.McDept;
import com.zmn.mcc.model.entity.dept.McDeptQuery;
import com.zmn.mcc.persistence.interfaces.dept.DeptDao;
import com.zmn.mcc.services.interfaces.dept.DeptService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author heguanghua
 * @since 2020/04/14 17:17
 */
@Service
public class DeptServiceImpl implements DeptService {

    @Resource
    private DeptDao deptDao;


    @Override
    public McDept findByKey(Integer deptId) {
        return deptDao.findByKey(deptId);
    }

    @Override
    public List<McDept> listPageByQuery(McDeptQuery query) {
        Integer count = this.countByQuery(query);
        query.setTotalCount(count);
        return count == 0 ? new ArrayList<>(0) : deptDao.listPageByQuery(query);
    }

    @Override
    public List<McDept> listByOrgId(Integer orgId) {
        return this.listByOrgId(orgId, null);
    }

    @Override
    public List<McDept> listByOrgId(Integer orgId, Integer status) {
        McDeptQuery query = new McDeptQuery();
        query.setOrgId(orgId);
        query.setStatus(status);
        return this.listByQuery(query);
    }

    @Override
    public List<McDept> listByOrgIds(List<Integer> orgIds, Integer status) {
        McDeptQuery query = new McDeptQuery();
        query.setOrgIds(orgIds);
        query.setStatus(status);
        return this.listByQuery(query);
    }

    @Override
    public List<McDept> listByRoleId(Integer roleId) {
        return this.listByRoleId(roleId, null);
    }

    @Override
    public List<McDept> listByRoleId(Integer roleId, Integer status) {
        McDeptQuery query = new McDeptQuery();
        query.setRoleId(roleId);
        query.setStatus(status);
        return this.listByQuery(query);
    }

    @Override
    public List<McDept> listByDpermitDeptId(Integer dpermitDeptId) {
        return this.listByDpermitDeptId(dpermitDeptId, null);
    }

    @Override
    public List<McDept> listByDpermitDeptId(Integer dpermitDeptId, Integer status) {
        McDeptQuery query = new McDeptQuery();
        query.setDpermitDeptId(dpermitDeptId);
        query.setStatus(status);
        return this.listByQuery(query);
    }

    @Override
    public List<McDept> listByParentId(Integer parentId) {
        return this.listByParentId(null, parentId, null);
    }

    @Override
    public List<McDept> listByParentId(Integer parentId, Integer status) {
        return this.listByParentId(null, parentId, null);
    }

    @Override
    public List<McDept> listByParentId(Integer orgId, Integer parentId, Integer status) {
        McDeptQuery query = new McDeptQuery();
        query.setOrgId(orgId);
        query.setParentId(parentId);
        query.setStatus(status);
        return this.listByQuery(query);
    }

    @Override
    public List<McDept> listByParentIds(List<Integer> deptIds, Integer status) {
        return deptDao.listByParentIds(deptIds, status);
    }

    @Override
    public List<McDept> listByQuery(McDeptQuery query) {
        return deptDao.listByQuery(query);
    }

    @Override
    public List<McDept> listDeptByOrgIdAndParentIds(Integer orgId, List<Integer> parentIdList, Integer level, Integer status) {
        McDeptQuery query = new McDeptQuery();
        query.setOrgId(orgId);
        query.setParentIdList(parentIdList);
        query.setLevel(level);
        query.setStatus(status);
        return this.listByQuery(query);
    }

    @Override
    public List<McDept> listValidDept() {
        return deptDao.listValidDept();
    }

    @Override
    public List<McDept> listValidZmnDept() {
        return deptDao.listValidZmnDept();
    }

    /**
     * 描述: 根据部门id集合查询角色Id
     *
     * @param deptIdList 部门id集合
     * @return List<DeptIdAndRoleId>
     * @author xujie
     * @since 2021/05/28
     **/
    @Override
    public List<DeptIdAndRoleId> listDeptIdAndRoleIdByDeptIdList(List<Integer> deptIdList) {

        if (deptIdList == null || deptIdList.size() == 0) {
            return new ArrayList<>();
        }
        return deptDao.listDeptIdAndRoleIdByDeptIdList(deptIdList);
    }

    /**
     * 根据组织id列表、部门ID列表，查询部门ID列表下的所有子部门
     *
     * @param orgIds  组织id列表
     * @param deptIds 部门ID列表
     * @return 所有子部门id、名称、状态
     * @author xujie
     * @since 2021-05-17
     */
    @Override
    public List<VtDTO> listSubDeptVtByOrgIdsAndDeptIds(List<Integer> orgIds, List<Integer> deptIds) {

        if (deptIds == null || deptIds.size() == 0) {
            return new ArrayList<>();
        }
        return deptDao.listSubDeptVtByOrgIdsAndDeptIds(orgIds, deptIds);
    }

    @Override
    public Integer countByQuery(McDeptQuery query) {
        return deptDao.countByQuery(query);
    }


    @Override
    public Integer updateByKey(McDept dept) {
        if (dept.getUpdateTime() == null) {
            dept.setUpdateTime(DateUtil.getNow());
        }
        return deptDao.updateByKey(dept);
    }

    @Override
    public Integer updateRoleId(Integer deptId, Integer roleId, String updater) {
        McDept dept = new McDept();
        dept.setDeptId(deptId);
        dept.setRoleId(roleId);
        dept.setUpdater(updater);
        return this.updateRoleId(dept);
    }

    @Override
    public Integer updateRoleId(McDept dept) {
        if (dept.getUpdateTime() == null) {
            dept.setUpdateTime(DateUtil.getNow());
        }
        return deptDao.updateRoleId(dept);
    }

    @Override
    public Integer updateStatus(Integer deptId, Integer status, String updater) {
        McDept dept = new McDept();
        dept.setDeptId(deptId);
        dept.setStatus(status);
        dept.setUpdater(updater);
        return this.updateStatus(dept);
    }

    @Override
    public Integer updateStatus(McDept dept) {
        if (dept.getUpdateTime() == null) {
            dept.setUpdateTime(DateUtil.getNow());
        }
        return deptDao.updateByKey(dept);
    }

    @Override
    public Integer updateStatusBatch(List<Integer> deptIds, Integer status, String updater) {
        StatusDTO dto = new StatusDTO();
        dto.setIds(deptIds);
        dto.setStatus(status.intValue());
        dto.setUpdater(updater);
        return updateStatusBatch(dto);
    }

    @Override
    public Integer updateStatusBatch(StatusDTO dto) {
        if (dto.getUpdateTime() == null) {
            dto.setUpdateTime(DateUtil.getNow());
        }
        return deptDao.updateStatusBatch(dto);
    }

    @Override
    public Integer updateSort(Integer deptId, Integer sort) {
        McDept dept = new McDept();
        dept.setDeptId(deptId);
        dept.setSort(sort);
        return this.updateSort(dept);
    }

    @Override
    public Integer updateSort(McDept dept) {
        return deptDao.updateSort(dept);
    }


    @Override
    public Integer insert(McDept dept) {
        if (NumberUtil.isNullOrZero(dept.getParentId())) {
            dept.setParentId(GlobalConsts.TOP_ID);
        }
        dept.setSort(100);
        dept.setStatus(GlobalDict.STATUS_ENABLE);
        if (dept.getUpdateTime() == null) {
            dept.setUpdateTime(DateUtil.getNow());
        }
        dept.setCreater(dept.getUpdater());
        dept.setCreateTime(dept.getUpdateTime());
        return deptDao.insert(dept);
    }


    @Override
    public Integer deleteByKey(Integer deptId) {
        return deptDao.deleteByKey(deptId);
    }

    @Override
    public List<McDept> listByBsDeptId(List<Integer> bsDeptIds) {
        if (CollectionUtil.isNullOrEmpty(bsDeptIds)) {
            return new ArrayList<>(0);
        }
        return deptDao.listByBsDeptId(bsDeptIds);
    }

    @Override
    public McDept findByBsDeptId(Integer bsDeptId) {
        if (NumberUtil.isNullOrZero(bsDeptId)) {
            return null;
        }
        return deptDao.findByBsDeptId(bsDeptId);
    }

    @Override
    public int countByBsDeptIdAndNotDeptId(Integer deptId, Integer bsDeptId) {
        return deptDao.countByBsDeptIdAndNotDeptId(deptId, bsDeptId);
    }

    @Override
    public void updateBsDeptIdByDeptId(Integer deptId, Integer bsDeptId, String updater) {
        deptDao.updateBsDeptIdByDeptId(deptId, bsDeptId, updater);
    }
}
